package com.dgut.service.serviceImpl;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dgut.common.OnOrOff;
import com.dgut.common.SelectRole;
import com.dgut.mapper.ContractMapper;
import com.dgut.pojo.Contract;
import com.dgut.service.ContractService;
import com.dgut.utils.PageRequestInfo;
import com.dgut.utils.PageResult;
import com.dgut.utils.PageUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

@Service
public class ContractServiceImpl implements ContractService {
    @Resource
    private ContractMapper contractMapper;


    /**
     * start-zwb
     */
    //获取合同列表
    public Object getContractList(PageRequestInfo pageRequestInfo){
        Page<Contract> page = new Page<>(pageRequestInfo.getPageNum(),pageRequestInfo.getPageSize());
        Page<Contract> resultPage = contractMapper.selectPage(page, null);
        PageResult result = PageUtils.getPageResult(resultPage);
        return result;
    }
    ///根据合同类型获取已上架的合同列表
    public Object getContractListOnShelves(PageRequestInfo pageRequestInfo,String type){
        Page<Contract> page = new Page<>(pageRequestInfo.getPageNum(),pageRequestInfo.getPageSize());
        QueryWrapper<Contract> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("state",OnOrOff.ON);
        if(type!=null)
            queryWrapper.like("type",type);
        Page<Contract> resultPage = contractMapper.selectPage(page, queryWrapper);
        PageResult result = PageUtils.getPageResult(resultPage);
        return result;
    }
    //根据合同名获取合同
    public Object getContractByName(String name) {
        QueryWrapper<Contract> queryWrapper = new QueryWrapper<>();
        if(name!=null)
            queryWrapper.like("name",name);
        List<Contract> contractList = contractMapper.selectList(queryWrapper);
        return contractList;
    }
    //根据类型获取合同
    public Object getContractByType(String type) {
        QueryWrapper<Contract> queryWrapper = new QueryWrapper<>();
        if(type!=null)
            queryWrapper.like("type",type);
        List<Contract> contractList = contractMapper.selectList(queryWrapper);
        return contractList;
    }
    //添加合同
    public Object addContract(Contract contract)
    {
        ConcurrentHashMap resultMap = new ConcurrentHashMap();
        contract.setState(OnOrOff.OFF);
        if(contractMapper.insert(contract)>0)
        {
            resultMap.put("message","更新成功");
            resultMap.put("result",true);
        }
        else {
            resultMap.put("message","更新失败");
            resultMap.put("result",false);
        }
        return resultMap;
    }
    //修改合同
    public Object modifyContract(Contract contract)
    {
        ConcurrentHashMap resultMap = new ConcurrentHashMap();
        if(contractMapper.updateById(contract) >0)
        {
            resultMap.put("message","更新成功");
            resultMap.put("result",true);
        }
        else {
            resultMap.put("message","更新失败");
            resultMap.put("result",false);
        }
        return resultMap;
    }
    //删除合同
    public Object deleteContract(Integer id)
    {
        ConcurrentHashMap resultMap = new ConcurrentHashMap();
        if(contractMapper.deleteById(id) >0)
        {
            resultMap.put("message","更新成功");
            resultMap.put("result",true);
        }
        else {
            resultMap.put("message","更新失败");
            resultMap.put("result",false);
        }
        return resultMap;
    }
    //上架合同
    public Object putOnContract(Integer id)
    {
        ConcurrentHashMap resultMap = new ConcurrentHashMap();
        QueryWrapper<Contract> queryWrapper = new QueryWrapper<>();
        Contract contract = contractMapper.selectById(id);
        if(contract!=null)
        {
            contract.setState(OnOrOff.ON);
            queryWrapper.eq("id",contract.getId());
            if(contractMapper.update(contract,queryWrapper)>0)
            {
                resultMap.put("message","更新成功");
                resultMap.put("result",true);
            }
            else {
                resultMap.put("message", "更新失败");
                resultMap.put("result", false);
            }
        }
        return resultMap;
    }
    //下架合同
    public Object takeDownContract(Integer id)
    {
        ConcurrentHashMap resultMap = new ConcurrentHashMap();
        QueryWrapper<Contract> queryWrapper = new QueryWrapper<>();
        Contract contract = contractMapper.selectById(id);
        if(contract!=null)
        {
            contract.setState(OnOrOff.OFF);
            queryWrapper.eq("id",contract.getId());
            if(contractMapper.update(contract,queryWrapper)>0)
            {
                resultMap.put("message","更新成功");
                resultMap.put("result",true);
            }
            else {
                resultMap.put("message", "更新失败");
                resultMap.put("result", false);
            }
        }
        return resultMap;
    }

    /**
     * end-zwb
     */


    /**
     * start-gxd
     */
    /**
     * end-gxd
     */


    /**
     * start-lzh
     */
    /**
     * end-lzh
     */

    /**
     * start-hwf
     */
    /**
     * end-hwf
     */

    /**
     * start-hqq
     */
    //根据合同模糊名称获取已上架的合同列表
    public Object getContractListOnShelvesByName(PageRequestInfo pageRequestInfo,String name) {
        QueryWrapper<Contract> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("state",OnOrOff.ON);
        if(name!=null) {
            queryWrapper.like("name", name);
        }
        Page<Contract> page = new Page<>(pageRequestInfo.getPageNum(),pageRequestInfo.getPageSize());
        Page<Contract> resultPage = contractMapper.selectPage(page, queryWrapper);
        PageResult result = PageUtils.getPageResult(resultPage);
        ConcurrentHashMap map = new ConcurrentHashMap();
        map.put("contractList",result);
        return map;
    }
    /**
     * end-hqq
     */
}
